package javaio12.piped;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

class Send implements Runnable
{
	OutputStream out = null;
	
	public Send()
	{
		super();
		this.out = new PipedOutputStream();
	}

	public OutputStream getOut()
	{
		return out;
	}

	public void setOut(OutputStream out)
	{
		this.out = out;
	}

	public void run()
	{
		String str = "hello world";
		byte [] c = str.getBytes();
		try
		{
			this.out.write(c);
		}
		catch(IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try
		{
			this.out.close();
		}
		catch(IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

class Receive implements Runnable
{
	InputStream in = null;
	
	public Receive()
	{
		super();
		this.in = new PipedInputStream();
	}

	public InputStream getIn()
	{
		return in;
	}

	public void setIn(InputStream in)
	{
		this.in = in;
	}

	public void run()
	{
		// TODO Auto-generated method stub
		try
		{
			int tem = 0;
			while((tem = this.in.read())!=-1)
					{
						//System.out.print(new Character((char)tem).toString());
						System.out.print((char)tem);
					}
		}
		catch(IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try
		{
			this.in.close();
		}
		catch(IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
public class PipedIOStream
{

	
	public static void main(String[] args)
	{
		Send s = new Send();
		Receive r = new Receive();
		try
		{
			((PipedOutputStream)(s.getOut())).connect((PipedInputStream)(r.getIn()));
		}
		catch(IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new Thread(s).start();
		new Thread(r).start();
	}

}
